﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace SafeNet.InitialDatabase.Repository
{
    public class SessionBuilderFactory
    {

        private static readonly ISessionBuilder m_ContextualBuilder = new ContextualSessionBuilder();

        public static ISessionBuilder GetBuilder()
        {
            return m_ContextualBuilder;
        }

        public static ISessionBuilder CurrentBuilder
        {
            get
            {
                ISessionBuilder CurrentSessionBuilder = default(ISessionBuilder);

                if (HttpContext.Current == null)
                {
                    //zar ovo nije suvisno?
                    if (CurrentSessionBuilder == null)
                    {
                        ISessionBuilder CurrentBuilderItem = GetBuilder();
                    }
                    //
                    return GetBuilder();
                }
                else
                {
                    CurrentSessionBuilder = HttpContext.Current.Application["SafeNet.InitialDatabase.Repository"] as ISessionBuilder;

                    if (CurrentSessionBuilder == null)
                    {
                        ISessionBuilder CurrentBuilderItem = GetBuilder();
                        HttpContext.Current.Application["SafeNet.InitialDatabase.Repository"] = CurrentBuilderItem;

                        return GetBuilder();
                    }
                }

                return CurrentSessionBuilder;
            }
        }
    }
}
